/*
Program: DC-SAR, File filefunc.c
Copyright (C) 2009 Davy De Coninck

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "conf.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <zlib.h>
#include <sys/stat.h>
#include "stringfunc.h"
#include "filefunc.h"
#include "sql.h"


int file_exists(char * fileName){
	struct stat buf;
   	int i = stat ( fileName, &buf );

	/* File found */
	if ( i == 0 )
	{
		return 1;
	}

	return 0;
}

int ProcessNonZipped(clsconfig *conf) {
	char compare[128];
	int lenstr;

	printf("Open path: %s\n", conf->logpath);

	if (chdir(conf->logpath) != 0){
		printf("No files read.\n");

		return 1;
	} else {

		struct dirent *dp;

		DIR *dir = opendir(conf->logpath);
		while ((dp=readdir(dir)) != NULL) 
		{
			char *tmp;
			tmp = path_cat(conf->logpath, dp->d_name);
			//printf("%s\n", tmp);
			strcpy(compare, substr(tmp,strlen(conf->logpath),6));
			if (strcmp(compare, "access") == 0)
			{
				lenstr = strlen(tmp);
				lenstr = lenstr - 2;
				strcpy(compare, substr(tmp,lenstr,2));
				
				if (strcmp(compare, "gz") == 0)
				{
					//zipped acceslog
					

				} else {
					//Non zipped acceslog
					ProcessAccesslog(conf, tmp);
				}
			}
			free(tmp);
			tmp=NULL;
		}

		printf("All files read.\n");

		closedir(dir);
		return 0;
	}
}

int ProcessAccesslog(clsconfig *Conf, char *tmp){
	FILE *ifp;
	char line [1000]; /* or other suitable maximum line size */
	char *mws[MAX_TOKENS];


	ifp = fopen(tmp, "r");

	if (ifp == NULL) {
	  fprintf(stderr, "Can't open file %s!\n",
		  tmp);
	  return 0;
	} else {
	  printf("Open file: %s\n", tmp);
	}

	openconnection(Conf);

	while (fgets(line, sizeof line, ifp) != NULL) /* read a line */
        {      
		//Row must be split
		str_split(line, mws, MAX_TOKENS, "");

		//Check if row exists
		if (strcmp(mws[0], Conf->lasttimestamp) <= 0) 
			continue;

		
		char sql[12000] = "";
		/*
		create table logbook(
		logbookid int auto_increment NOT NULL
		Timestamp datetime NOT NULL
		Elapsed bigint NOT NULL
		Client varchar(255) NOT NULL
		Action/code varchar(1000) NOT NULL
		Size bigint NOT NULL
		Method varchar(255) NOT NULL
		URI varchar(10000) NOT NULL
		Ident varchar(255) NOT NULL
		Hierarchy varchar(255) NOT NULL
		Content varchar(255) NOT NULL);
		*/
		strcat(sql, "INSERT INTO logbook (Timestamp, elapsed, client, Action_code, Size, method, URI, Ident, Hierarchy, ");
		strcat(sql, "Content) VALUES (FROM_UNIXTIME(");
		strcat(sql, mws[0]);
		strcat(sql, "),");
		strcat(sql, mws[1]);
		strcat(sql, ",'");
		strcat(sql, mws[2]);
		strcat(sql, "','");
		strcat(sql, mws[3]);
		strcat(sql, "',");
		strcat(sql, mws[4]);
		strcat(sql, ",'");
		strcat(sql, mws[5]);
		strcat(sql, "','");
		strcat(sql, mws[6]);
		strcat(sql, "','");
		strcat(sql, mws[7]);
		strcat(sql, "','");
		strcat(sql, mws[8]);
		strcat(sql, "','");
		strcat(sql, mws[9]);
		strcat(sql, "')");

		printf("%s\n", sql);
		printf("====================================================================\n",sql);

		Get_Query(sql);
	}

	Update_Value("lasttimestamp", mws[0]);

	closeconnection;

	fclose (ifp);


	return -1;
}

char *path_cat (const char *str1, char *str2) {
	size_t str1_len = strlen(str1);
	size_t str2_len = strlen(str2);
	char *result;
	result = malloc((str1_len+str2_len+1)*sizeof *result);
	strcpy (result,str1);
	int i,j;
	for(i=str1_len, j=0; ((i<(str1_len+str2_len)) && (j<str2_len));i++, j++) {
		result[i]=str2[j];
	}
	result[str1_len+str2_len]='\0';
	return result;
}
